﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO_HealthInsurance;
using BLL_HealthInsurance;
using System.Data;

namespace HealthInsuranceMgm.Admin
{
    public partial class News : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            load();
            PanelUpdatenews.Visible = false;
            PanelListNew.Visible = true;
            PanelinsertNew.Visible = true;
        }
       
        protected void btnNew_Click(object sender, EventArgs e)
        {
BLLNews objBll = new BLLNews();
   DTONews  dot = new DTONews();
            string title = txttitle.Text;
            string content = FCKeditor1.Value.ToString();
            
            dot.Title = title;
            dot.Description1 = content;
            objBll.insertnew(dot);
        }
        public void load()
        {
            BLLNews bll = new BLLNews();
            grvNew.DataSource = bll.Listnew();
            grvNew.DataBind();
        }

        public void LoadData(int newid)
        {
            
            BLLNews obj = new BLLNews();
            DTONews objDt = new DTONews();

            objDt.Id = newid;
            DataTable dt = obj.GetNewById(objDt);

            HiddenField1.Value = dt.Rows[0]["id"].ToString();
            txttle.Text = dt.Rows[0]["title"].ToString();
            FCKeditor2.Value = dt.Rows[0]["description"].ToString();
          

        }
        protected void grvNew_Sorting(object sender, GridViewSortEventArgs e)
        {
            BLLNews obj = new BLLNews();
            string sortExpression = e.SortExpression;
            string Direction = "";

            if (SortDirection == SortDirection.Ascending)
            {
                SortDirection = SortDirection.Descending;
                Direction = "DESC";
            }
            else
            {
                SortDirection = SortDirection.Ascending;
                Direction = "ASC";
            }
            DataTable dt = obj.Listnew();
            dt.DefaultView.Sort = sortExpression + " " + Direction;
            grvNew.DataSource = dt;
            grvNew.DataBind();
        }
        public void message(string script)
        {

            Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", script, true);
        }
        protected void grvNew_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            int newid = Int32.Parse(grvNew.DataKeys[e.NewSelectedIndex].Value.ToString());
            LoadData(newid);


            PanelinsertNew.Visible = false;
            PanelListNew.Visible = false;
            PanelUpdatenews.Visible = true;
        }

        protected void grvNew_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int newId = (int)grvNew.DataKeys[e.RowIndex].Value;
            BLLNews obj = new BLLNews();
            DTONews g = new DTONews();
            g.Id = newId;
            obj.deleteNews(g);
            load();
        }

        protected void grvNew_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
                l.Attributes.Add("onclick", "javascript: return " +
                "confirm('Are you sure you want to delete this record " +
                DataBinder.Eval(e.Row.DataItem, "id") + "')");

            }
        }

        protected void grvNew_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            BLLNews obj = new BLLNews();
            DTONews objDt = new DTONews();
            if (e.CommandName == "Delete")
            {
              
                int newid = Convert.ToInt32(e.CommandArgument);
           
                objDt.Id = newid;
                obj.deleteNews(objDt);
           
            }
        }

        protected void grvNew_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvNew.PageIndex = e.NewPageIndex;
            load();
        }
        public SortDirection SortDirection
        {
            get
            {

                if (ViewState["SortDirection"] == null)
                {
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortDirection"];
            }
            set
            {
                ViewState["SortDirection"] = value;
            }
        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {

            int newId = Int32.Parse(HiddenField1.Value.ToString());
            string title = txttle.Text;
            string content = FCKeditor2.Value;
           

            if (IsValid)
            {
                DTONews objdto = new DTONews();
                BLLNews objbll = new BLLNews();
                objdto.Id = newId;
                objdto.Title = title;
                objdto.Description1 = content;
               
                int i = objbll.updateNews(objdto);
                load();
                if (i > 0)
                {
                    message("alert('Update Successfull');");
                }


            }
        }
       

        
    }
}